const urlObj = myGetUrlVal();

if(urlObj.id !== undefined){
    getNamePwd();
}

$('form').submit(function(e){
    e.preventDefault();

    let name = $('[name="name"]').val();
    let pwd = $('[name="pwd"]').val();

    login(name,pwd);
})

async function getNamePwd(){
    const response = await myPromiseJQueryAjax('../server/goods_select_id.php','get',{id:urlObj.id},'json');

    console.log(response);

    $('[name="name"]').val(response.name);
    $('[name=pwd]').val(response.pwd);
}

async function login(name,pwd){
    const response = await myPromiseJQueryAjax('../server/goods_login.php','post',{userName:name,userPwd:pwd},'json');

    console.log(response);

    if(response.code === 0){
        $('[name=msg]').html('登录失败 您输入的账号密码有误').css({color:'red'});
    }else if(response.code === 1){
        mySetCookie('login',1,7 * 24 * 60 * 60,'/');

        let num = 5;

        $('[name="msg"]').html(`恭喜您 登录成功 ${num}秒后跳转首页`).css({color:'blue'});

        let time = setInterval(() => {
            num--;
            $('[name="msg"]').html(`恭喜您 登录成功 ${num}秒后跳转首页`).css({color:'blue'});
            if(num === 1){
                clearInterval(time);

                if(window.location.search.substring(1,4) === 'url'){
                    window.location.href = window.location.search.substring(5);
                }else{
                    window.location.href = '../pages/index.html';
                }
            }
        }, 1000);
    }
}

